package com.wanxi.controller;

import com.wanxi.model.CompanyModel;
import com.wanxi.model.ResultModel;
import com.wanxi.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
 * @author 杨金乾
 */
@Controller
@CrossOrigin(origins = "*")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @Autowired
    SendMessageController sendMessageController;
    private String table="公司";
    @RequestMapping("/back/company/add")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel companyAdd(CompanyModel model) throws  IOException {

        ResultModel resultModel =companyService.add(model);
        sendMessageController.addUserMessages(table);
//        6.返回给前端
        return resultModel;
    }

    @RequestMapping("/back/company/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel companyDelete(CompanyModel model) throws  IOException {

        ResultModel resultModel = companyService.delete(model);
        sendMessageController.deleteUserMessages(table);
//        6.返回给前端
        return resultModel;
    }

    @RequestMapping("/back/company/findAll")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel companyFindAll(CompanyModel model) throws  IOException {

        ResultModel resultModel = companyService.findAll(model);
        sendMessageController.findAllUserMessages(table);
//        6.返回给前端
        return resultModel;
    }

    @RequestMapping("/back/company/findById")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel companyFindById(CompanyModel model) throws  IOException {

        ResultModel resultModel=companyService.findById(model);
        sendMessageController.findByIdUserMessages(table);
//        6.返回给前端
        return resultModel;
    }

    @RequestMapping("/back/company/isShow")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')or hasAuthority('unaccessible')")
    protected ResultModel companyUpdateIsShow(CompanyModel model) throws  IOException {

        ResultModel resultModel = companyService.isShow(model);
        sendMessageController.updateIsShowMessages(table);
//        6.返回给前端
        return resultModel;
    }

    @RequestMapping("/back/company/update")
    @ResponseBody
    @PreAuthorize("hasAuthority('subPermission') or hasAuthority('allPermission')")
    protected ResultModel companyUpdate(CompanyModel model) throws  IOException {

        ResultModel resultModel = companyService.update(model);
        sendMessageController.updateMessages(table);
//        6.返回给前端
        return resultModel;
    }
}
